'use strict'

const _ = require('lodash')
const { Controller } = require('egg')

class Test extends Controller {
  async test() {
    const { ctx } = this
    const { action } = ctx.params
    const fnName = _.camelCase(action)
    console.log('fnName', fnName)

    if (this[fnName]) return await this[fnName]()
  }

  async autoUpdateTask() {
    const { ctx } = this
    const { query } = ctx

    const returnData = await this.service.clockIn.autoUpdateTask()

    ctx.body = {
      code: 0,
      msg: 'success',
      data: returnData,
    }
  }

  async taskPushMessage() {
    const { ctx } = this
    const { query } = ctx

    try {
      await this.service.clockIn.taskPushMessage()
    } catch (err) {
      console.error(err)
    }

    ctx.body = {
      code: 0,
      msg: 'success',
    }
  }

  async checkExpiredTasks() {
    const { ctx } = this

    try {
      await this.service.task.checkExpiredTasks()
    } catch (err) {
      console.error(err)
    }

    ctx.body = {
      code: 0,
      msg: 'success',
    }
  }
}

module.exports = Test
